//
//  YXTradingListView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/25.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXTradingListView.h"
#import "YXNameTableView.h"
#import "YXTurnoverTableView.h"
#import "YXPartnerHeaderView.h"
#import "YXHomeViewModel.h"
@interface YXTradingListView ()
@property (nonatomic ,strong) YXPartnerHeaderView *headerView;
@property (nonatomic ,strong) YXNameTableView *nameTableView;
@property (nonatomic ,strong) YXTurnoverTableView *turnoverTableView;

@property (nonatomic ,strong) NSString *pid; // 存储当前ID
@property (nonatomic ,strong) NSString *type; // 存储当前类型
@property (nonatomic ,strong) NSString *data; // 存储时间
@end

@implementation YXTradingListView

- (void)setNameDataArr:(NSArray *)nameDataArr {
    _nameDataArr = nameDataArr;
    self.nameTableView.nameArr = self.nameDataArr;
    [self.nameTableView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      

        [self createHeaderViewUI];
        [self createTableViewUI];
    }
    return self;
}

- (void)createHeaderViewUI {
    
    // 默认按月查询
    self.data = @"true";
    
    YXPartnerHeaderView *headreView = [[YXPartnerHeaderView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 110))];
    headreView.type = @"交易列表";
    [self addSubview:headreView];
    [headreView setSelectTimeBlock:^(NSInteger index) {
        if (index == 0) {
            self.data = @"true";
        }else {
            self.data = @"false";
        }
//          [self requestDataPid:@"-1" Data:self.data Typ:@"-1"];
        [self requestDataPid:self.pid == nil ? @"-1" : self.pid Data:self.data Typ:self.type == nil ? @"-1" : self.type];
    }];
}

- (void)requestDataPid:(NSString *)pid Data:(NSString *)data Typ:(NSString *)type {
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    [self.turnoverTableView.turnoverArr removeAllObjects];
    [YXHomeViewModel getQueryfindRecordsofConsumptionSessionId:[YXUserInfoManager getUserInfo].sessionId Pid:pid PageNo:@"1" typ:type Data:data ModelList:^(NSArray *listArr) {
        if ([data isEqualToString:@"false"]) {
            listArr=(NSMutableArray *)[[listArr reverseObjectEnumerator] allObjects];
        }
        self.turnoverTableView.turnoverArr = [NSMutableArray arrayWithArray:listArr];
        [self.turnoverTableView reloadData];
        [SVProgressHUD dismiss];
    } Failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [self showAlertWithTitle:@"提示" Message:@"维护中，请稍等！"];
    }];
    
}


- (void)createTableViewUI {

    UIView *sectionView = [self createSectionView];
    [self addSubview:sectionView];
    
    self.nameTableView = [[YXNameTableView alloc] initWithFrame:(CGRectMake(0, sectionView.H + sectionView.Y, 100, kScreenHeight-224)) style:(UITableViewStylePlain)];
    [self addSubview:self.nameTableView];
 
    self.turnoverTableView = [[YXTurnoverTableView alloc] initWithFrame:(CGRectMake(100, sectionView.H + sectionView.Y, kScreenWidth-100 , kScreenHeight-224)) style:(UITableViewStylePlain)];
    [self addSubview:self.turnoverTableView];

    
    YXWeakSelf
    [self.nameTableView setNameDidSelectHandlerTwo:^(NSString *pid, NSString *type) {
        weakSelf.pid = pid;
        weakSelf.type = type;
        [weakSelf requestDataPid:pid Data:weakSelf.data Typ:type];
    }];
    
    
    UIView *lineView = [[UIView alloc] initWithFrame:(CGRectMake(0, self.nameTableView.H + self.nameTableView.Y, kScreenWidth, 1))];
    lineView.backgroundColor = color_lineColor;
    [self addSubview:lineView];
}

- (UIView *)createSectionView {
    
    UIView *sectionView = [[UIView alloc] initWithFrame:(CGRectMake(0, 110, kScreenWidth, 50))];
    sectionView.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView1 = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 1))];
    lineView1.backgroundColor = color_lineColor;
    [sectionView addSubview:lineView1];
    
    UIView *lineView2 = [[UIView alloc] initWithFrame:(CGRectMake(0, sectionView.H-1, kScreenWidth, 1))];
    lineView2.backgroundColor = color_lineColor;
    [sectionView addSubview:lineView2];
    
    UILabel *nameLab = [UILabel setLabelWithText:@"姓名" fontSize:16 color:color_textOne];
    nameLab.frame = CGRectMake(0, 0, 100, 50);
    nameLab.textAlignment = NSTextAlignmentCenter;
    [sectionView addSubview:nameLab];

    UILabel *timeLab = [UILabel setLabelWithText:@"月份" fontSize:16 color:color_textOne];
    timeLab.frame = CGRectMake((kScreenWidth-100)/2, 0, 100, 50);
    timeLab.textAlignment = NSTextAlignmentCenter;
    [sectionView addSubview:timeLab];

    UILabel *turnoverLab = [UILabel setLabelWithText:@"交易额" fontSize:16 color:color_textOne];
    turnoverLab.frame = CGRectMake(kScreenWidth-100, 0, 100, 50);
    turnoverLab.textAlignment = NSTextAlignmentCenter;
    [sectionView addSubview:turnoverLab];

    return sectionView;
}


@end
